package com.cpit.platform.security.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.cpit.platform.common.constant.SystemConstant;
import com.cpit.platform.common.constant.UserConstant;
import com.cpit.platform.dao.RoleRepository;
import com.cpit.platform.dao.UserToRoleRepository;
import com.cpit.platform.dto.Authority;
import com.cpit.platform.dto.Role;
import com.cpit.platform.dto.User;
import com.cpit.platform.dto.UserToRole;
import com.cpit.platform.log.LogOperator;
import com.cpit.platform.service.AuthorityService;
import com.cpit.platform.service.UserService;

public class CustomUserService implements UserDetailsService {
	
	@Autowired 
	private UserService userService; 
	
	@Autowired
	private AuthorityService authorityService;
	@Autowired
	private UserToRoleRepository userToRoleRepository;
	
	@Autowired
	private RoleRepository roleRepository;
	private final String loggerName = this.getClass().getName();
	
	@Override
	public UserDetails loadUserByUsername(String arg0) throws UsernameNotFoundException {
		LogOperator.debug(loggerName, "当前的用户名是：" + arg0);
		if(arg0.equals("admin")) {
			LogOperator.debug(loggerName, "current user is: admin");
			User adminUser = new User();
			User uTmp = userService.findByUsername(arg0);
			if(uTmp == null)
				adminUser.setId(1);
			else {
				adminUser.setId(uTmp.getId());
			}
			adminUser.setUsername("admin");
			adminUser.setDisplayName(uTmp.getDisplayName());
			adminUser.setAuthType(UserConstant.USER_AUTH_TYPE_SYSTE_ADMIN);
			adminUser.setOrganizationId(SystemConstant.ORGANIZATION_ID_ROOT);
			adminUser.setOrganizationCode(SystemConstant.ORGANIZATION_CODE_ROOT);
			List<Authority> allAuths = authorityService.findAll();
			UserInfo userInfo = new UserInfo(adminUser);  
			userInfo.setUsername("admin");  
			userInfo.setName("admin");
			if(uTmp == null)
				userInfo.setPassword("admin");
			else {
				userInfo.setPassword(uTmp.getPassword());adminUser.setId(uTmp.getId());
			}
			Set<AuthorityInfo> authorities = new HashSet<AuthorityInfo>();
			if(allAuths != null && allAuths.size() > 0) {
				for(Authority a : allAuths) {
					AuthorityInfo authorityInfo = new AuthorityInfo(a.getAuthorityContent()); 
					authorities.add(authorityInfo);  
				}
			}
			userInfo.setAuthorities(authorities);  
			return userInfo;  
		} else {
			User uTmp = userService.findByUsername(arg0);
			if(uTmp == null) {
				throw new UsernameNotFoundException("user not found");
			}
			UserInfo userInfo = new UserInfo(uTmp);
			Set<AuthorityInfo> authorities = new HashSet<AuthorityInfo>();
			List<UserToRole> allRoles = userToRoleRepository.findByUserId(uTmp.getId());
			if(allRoles != null && allRoles.size() > 0) {
				for(UserToRole rr : allRoles) {
					Role r = roleRepository.findById(rr.getRoleId());
					List<Authority> allAuths = r.getAuthorities();
					if(allAuths != null && allAuths.size() > 0) {
						for(Authority a : allAuths) {
							AuthorityInfo authorityInfo = new AuthorityInfo(a.getAuthorityContent()); 
							authorities.add(authorityInfo);  
						}
					}
				}
			}
			userInfo.setAuthorities(authorities);
			userInfo.setUsername(uTmp.getUsername());  
			userInfo.setName(uTmp.getDisplayName());
			userInfo.setPassword(uTmp.getPassword());
			LogOperator.debug(loggerName, "authority get user result is: " + userInfo);
			return userInfo;
		}
	}

}
